package edu.caece.langprocessor.lexical;

import java.io.IOException;
import java.io.Reader;

public class PositionalFileReaderDecorator extends Reader {
	private Integer line;
	private Integer column;
	private Reader internalReader;

	public PositionalFileReaderDecorator(Reader reader) {
		this.line = 1;
		this.column = 0;
		this.internalReader = reader;
	}

	@Override
	public void close() throws IOException {
		this.internalReader.close();
	}

	@Override
	public int read(char[] cbuf, int off, int len) throws IOException {
		int readValue;
		//TODO implementar el conteo de \n y lineas correcto
		//la cuenta deberia ser algo como: 
		//cantidad de \n sobre cantidad de caracteres leidos
		
		readValue = this.internalReader.read(cbuf, off, len);

		if (readValue != -1) {
			// '\n':
			//this.line++;
			//this.column = 0;
		}
		return readValue;
	}

	@Override
	public int read() throws IOException {
		int readValue;

		readValue = this.internalReader.read();

		if (readValue != -1) {
			char c = (char) readValue;
			switch (c) {
			case '\n':
				this.line++;
				this.column = 0;
				break;
			default:
				this.column++;
				break;
			}
		}
		return readValue;
	}

	public Integer getLine() {
		return line;
	}

	public Integer getColumn() {
		return column;
	}
}